layui.use(['layedit', 'form','layer'], function(){
    var form = layui.form;
    var layedit = layui.layedit;
    var layer=layui.layer;

    //获取当前时间字符串
    function getNowTime() {
        var myDate = new Date;
        var year = myDate.getFullYear(); //获取当前年
        var mon = myDate.getMonth() + 1; //获取当前月
        var date = myDate.getDate();//获取当前日
        var h = myDate.getHours();//获取当前小时数(0-23)
        var m = myDate.getMinutes();//获取当前分钟数(0-59)
        var nowTime = year + '-' + mon + '-' + date + '  ' + h + ':' + m;
        return nowTime;
    }

    var loginId_p=$.cookie('loginAdminId');//获取cookie
    if (loginId_p==null||loginId_p=='null'){//判断是否登陆 =null=没登陆
        layer.alert("您还未登录",function () {
            window.location.replace("/games_info_sys/backstage_web/login.html");
        });
    }else if(atob(loginId_p).indexOf("Logined:")==0) {//登陆状态中
        //创建一个富文本编辑器，
        var index = layedit.build('infoContent',{
            hideTool:['image']
            ,height: 350    //设置编辑器的高度
        });
        //提交时把值同步到文本域中
        form.verify({
            //content富文本域中的lay-verify值
            content: function(value) {
                return layedit.sync(index);
            }
        });

        $('#addInfobut').click(function () {
            var infoAuthor=utf8to16(atob(loginId_p)).substring(8,utf8to16(atob(loginId_p)).length);//解密字符串获取用户名作为作者名
            var gamesInfo={infoTitle:$('#infoTitle').val(),infoContent:layedit.getContent(index),infoTime:getNowTime(),infoSource:$('#infoSource').val(),infoAuthor:infoAuthor};
            $.ajax({
                type: "POST",
                contentType: "application/json;charset=UTF-8",
                url: "/games_info_sys/info/add.do",
                data: JSON.stringify(gamesInfo),
                dataType: 'json',
                success: function(result) {
                    if(result.data == "1") {
                        layer.msg('文章发布成功！');
                    } else {
                        layer.msg('文章发布失败!');
                    }
                }
            });
        })
    }
});